/**
 * 物料定义
 */
Ext.define('YX.sbas.BaseMaterial', {
    extend: 'Ext.panel.Panel',
    require: ['UX.codeview.FieldX'],
    layout: {
        type: 'hbox',
        align: 'stretch'
    },
    gridMat: null,
    pelLeft: null,
    txtSearchMatCode: null,
    frmSearch: null,
    txtMatCode: null,
    txtMatVer: null,
    txtMatDesc: null,
    cmbMatType: null,
    cmbMatUnit: null,
    numPrice: null,
    txtPriceLevel: null,
    chkNeedIqc: null,
    sfSupCode: null,
    sfSupMat: null,
    btnSave: null,
    btnDelete: null,
    btnSearch: null,
    btnReset: null,
    frmOperate: null,
    initComponent: function () {
        //列表
        this.gridMat = Ext.create('Ext.grid.Panel', {
            flex: 1,
            columns: [{
                text: T.label.matCode,
                dataIndex: 'matCode',
                width: 120
            }, {
                text: T.label.ver,
                dataIndex: 'matVer',
                width: 50
            }, {
                text: T.label.matDesc,
                dataIndex: 'matDesc',
                width: 200
            }, {
                text: T.label.matType,
                dataIndex: 'matType',
                width: 70
            }, {
                text: T.label.unit,
                dataIndex: 'matUnit',
                width: 50
            }, {
                xtype: 'numbercolumn',
                text: T.label.matPrice,
                dataIndex: 'matPrice',
                format: '0,000',
                width: 70
            }, {
                text: T.label.priceLevel,
                dataIndex: 'priceLevel',
                width: 60
            }, {
                text: T.label.needIqcCheck,
                dataIndex: 'needIqcCheck',
                width: 50
            }, {
                text: T.label.supCode,
                dataIndex: 'supCode',
                width: 80
            }, {
                text: T.label.supMatCode,
                dataIndex: 'supMatCode',
                width: 80
            }],
            store: {
                fields: ['matCode', 'matVer', 'matDesc', 'matType', 'matUnit', 'matPrice', {
                    type: 'number',
                    name: 'matPrice'
                }, 'priceLevel', 'needIqcCheck', 'supCode', 'supMatCode'],
                proxy: {
                    type: 'zbusrpc',
                    module: 'SbasViewBaseMaterialService',
                    reader: {
                        root: 'matList'
                    }
                }
            },
            listeners: {
                scope: this,
                select: this.onGridMatSelect
            }
        });

        //查询
        this.txtSearchMatCode = Ext.create('Ext.form.field.Text', {
            fieldLabel: T.label.matCode,
            labelWidth: 80,
            anchor: '98%'
        });
        this.frmSearch = Ext.create('Ext.form.Panel', {
            title: T.label.search,
            layout: {
                type: 'anchor',
                defaultAnchor: '98%'
            },
            items: [this.txtSearchMatCode]
        });

        //操作
        this.txtMatCode = Ext.create('Ext.form.field.Text', {
            fieldLabel: T.label.matCode,
            allowBlank: false,
            maxLength: 30,
            labelWidth: 80,
            anchor: '98%',
            name: 'matCode'
        });
        this.txtMatVer = Ext.create('Ext.form.field.Text', {
            fieldLabel: T.label.ver,
            allowBlank: false,
            maxLength: 10,
            labelWidth: 80,
            value: '1',
            anchor: '98%',
            name: 'matVer'
        });
        this.txtMatDesc = Ext.create('Ext.form.field.Text', {
            fieldLabel: T.label.matDesc,
            maxLength: 100,
            labelWidth: 80,
            anchor: '98%',
            name: 'matDesc'
        });
        this.cmbMatType = Ext.create('Ext.form.field.ComboBox', {
            fieldLabel: T.label.matType,
            labelWidth: 80,
            anchor: '98%',
            editable: false,
            name: 'matType',
            displayField: 'data1',
            valueField: 'key1',
            store: Ext.create('Ext.data.JsonStore', {
                fields: ['key1', 'data1'],
                proxy: {
                    type: 'zbusrpc',
                    module: 'BasViewCommonDataSetupDetailService',
                    extraParams: {
                        groupName: 'MATERIAL_TYPE'
                    },
                    reader: {
                        type: 'json',
                        root: 'cdsDtlList'
                    }
                }
            })
        });
        this.cmbMatUnit = Ext.create('Ext.form.field.ComboBox', {
            fieldLabel: T.label.unit,
            labelWidth: 80,
            anchor: '98%',
            editable: false,
            name: 'matUnit',
            displayField: 'data1',
            valueField: 'key1',
            store: Ext.create('Ext.data.JsonStore', {
                fields: ['key1', 'data1'],
                proxy: {
                    type: 'zbusrpc',
                    module: 'BasViewCommonDataSetupDetailService',
                    extraParams: {
                        groupName: 'MATERIAL_UNIT'
                    },
                    reader: {
                        type: 'json',
                        root: 'cdsDtlList'
                    }
                }
            })
        });
        this.numPrice = Ext.create('Ext.form.field.Number', {
            fieldLabel: T.label.matPrice,
            labelWidth: 80,
            anchor: '98%',
            minValue: 0,
            value: 0,
            name: 'matPrice'
        });
        this.txtPriceLevel = Ext.create('Ext.form.field.Text', {
            fieldLabel: T.label.priceLevel,
            labelWidth: 80,
            anchor: '98%',
            maxLength: 5,
            name: 'priceLevel'
        });
        this.chkNeedIqc = Ext.create('Ext.form.field.Checkbox', {
            fieldLabel: T.label.needIqcCheck,
            name: 'needIqcCheck'
        });
        this.sfSupCode = Ext.widget('codeviewfieldx', {
            columns: [{
                text: T.label.supCode,
                dataIndex: 'supCode',
                flex: 1
            }, {
                text: T.label.supName,
                dataIndex: 'supName',
                flex: 1
            }],
            store: Ext.create('Ext.data.JsonStore', {
                fields: ['supCode', 'supName'],
                proxy: {
                    type: 'zbusrpc',
                    module: 'SbasViewSupplierRosterService',
                    reader: {
                        type: 'json',
                        root: 'supplierList'
                    }
                }
            }),
            fieldLabel: T.label.supCode,
            labelWidth: 80,
            anchor: '98%',
            name: 'supCode'
        });
        /*Ext.create('YX.form.Search', {
            fieldLabel: T.label.supCode,
            labelWidth: 80,
            anchor: '98%',
            readOnly: true,
            name: 'supCode',
            showSecondField: true,
            module: 'SbasViewSupplierRosterService',
            root: 'supplierList',
            columns: [{
                text: T.label.supCode,
                dataIndex: 'supCode',
                flex: 1
            }, {
                text: T.label.supName,
                dataIndex: 'supName',
                flex: 1
            }],
            fields: ['supCode', 'supName']
        });
        */

        this.sfSupMat = Ext.widget('codeviewfieldx', {
            columns: [{
                text: T.label.matCode,
                disabledSearch: false,
                dataIndex: 'supMatCode'
            }, {
                text: T.label.supCode,
                disabledSearch: false,
                dataIndex: 'supCode'
            }],
            store: Ext.create('Ext.data.JsonStore', {
                fields: ['supCode', 'supMatCode'],
                proxy: {
                    type: 'zbusrpc',
                    module: 'SbasViewSupplierMaterialService',
                    reader: {
                        type: 'json',
                        root: 'supMatList'
                    }
                }
            }),
            fieldLabel: T.label.supMatCode,
            labelWidth: 80,
            anchor: '98%',
            name: 'supMatCode',
            listeners: {
                scope: this,
                beforestoreload: this.onSupMatCodeBeforeStoreLoad
            }
        });
        /*Ext.create('YX.form.Search', {
        fieldLabel: T.label.supMatCode,
        labelWidth: 80,
        anchor: '98%',
        readOnly: true,
        name: 'supMatCode',
        module: 'SbasViewSupplierMaterialService',
        root: 'supMatList',
        columns: [{
            text: T.label.matCode,
            dataIndex: 'supMatCode'
        }, {
            text: T.label.supCode,
            dataIndex: 'supCode'
        }],
        fields: ['supCode', 'supMatCode'],
        listeners: {
            scope: this,
            beforeShow: this.onSfSupMatBeforeShow
        }
    });*/
        this.btnSave = Ext.create('Ext.button.Button', {
            text: T.label.save,
            itemId: 'btnSave',
            iconCls: 'icon-disk',
            scope: this,
            handler: this.onBtnSaveClicked
        });
        this.btnDelete = Ext.create('Ext.button.Button', {
            text: T.label.del,
            itemId: 'btnDelete',
            iconCls: 'icon-delete',
            scope: this,
            handler: this.onBtnDeleteClicked
        });
        this.btnSearch = Ext.create('Ext.button.Button', {
            text: T.label.search,
            itemId: 'btnSearch',
            iconCls: 'icon-find',
            scope: this,
            handler: this.onBtnSearchClicked
        });
        this.btnReset = Ext.create('Ext.button.Button', {
            text: T.label.reset,
            itemId: 'btnReset',
            iconCls: 'icon-clear',
            scope: this,
            handler: this.onBtnResetClicked
        });
        this.frmOperate = Ext.create('Ext.form.Panel', {
            title: T.label.operate,
            flex: 1,
            items: [this.txtMatCode, this.txtMatVer, this.txtMatDesc, this.cmbMatType, this.cmbMatUnit, this.numPrice,
                this.txtPriceLevel, this.chkNeedIqc, this.sfSupCode, this.sfSupMat],
            bbar: {
                items: [this.btnSave, this.btnDelete, this.btnSearch, this.btnReset]
            }
        });

        this.items = [{
            title: T.label.material,
            flex: 1,
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            items: [this.gridMat],
            bbar: {
                xtype: 'pagingtoolbar',
                displayInfo: true,
                store: this.gridMat.getStore()
            }
        }, {
            width: 260,
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            items: [this.frmSearch, this.frmOperate]
        }];

        this.callParent();
    },
    onBtnSaveClicked: function (btn) {
        if (!this.frmOperate.getForm().isValid()) {
            return;
        }

        var me = this;
        Ext.Msg.show({
            title: T.label.save,
            msg: T.msg.are_you_sure_to_save_data,
            icon: Ext.Msg.QUESTION,
            buttons: Ext.Msg.YESNO,
            fn: function (btnId) {
                if (btnId == 'yes') {
                    btn.disable();

                    var parms = me.frmOperate.getValues();
                    parms.needIqcCheck = me.chkNeedIqc.getValue() ? "Y" : "N";

                    YX.ZbusRpc.request({
                        module: 'SbasSaveBaseMaterialService',
                        params: parms,
                        callback: function () {
                            btn.enable();
                        },
                        success: function () {
                            me.frmOperate.getForm().reset();
                            me.gridMat.getStore().loadPage(1);
                        }
                    });
                }
            }
        });
    },
    onBtnDeleteClicked: function (btn) {
        var rcds = this.gridMat.getSelectionModel().getSelection();
        if (Ext.isEmpty(rcds)) {
            return;
        }

        var me = this;
        Ext.Msg.show({
            title: T.label.save,
            msg: T.msg.are_you_sure_to_delete_data,
            icon: Ext.Msg.QUESTION,
            buttons: Ext.Msg.YESNO,
            fn: function (btnId) {
                if (btnId == 'yes') {
                    btn.disable();

                    YX.ZbusRpc.request({
                        module: 'SbasSaveBaseMaterialService',
                        params: {
                            matCode: rcds[0].get('matCode'),
                            matVer: rcds[0].get('matVer'),
                            matStatus: 'D'
                        },
                        callback: function () {
                            btn.enable();
                        },
                        success: function () {
                            me.frmOperate.getForm().reset();
                            me.gridMat.getStore().loadPage(1);
                        }
                    });
                }
            }
        });
    },
    onBtnSearchClicked: function (btn) {
        var vMatCode = this.txtSearchMatCode.getValue();
        this.gridMat.getStore().load({
            params: {
                matCode: vMatCode
            }
        });
    },
    onBtnResetClicked: function (btn) {
        this.frmOperate.getForm().reset();
    },
    onSupMatCodeBeforeStoreLoad: function (field, store, params, selected) {
        if (this.sfSupCode.getValue() === '') {
            return false;
        }

        params.supCode = this.sfSupCode.getValue();

        return true;
    },
    onGridMatSelect: function (sm, record) {
        this.frmOperate.loadRecord(record);
        this.chkNeedIqc.setValue(record.get('needIqcCheck') === 'Y');
    }
});